package com.ms.p2p.controller.admin;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.common.utils.MsDateUtils;
import com.ms.common.utils.excel.ExcelData;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.controller.BaseController;
import com.ms.p2p.provider.DictionaryProvider;
import com.ms.p2p.provider.LoanCategoryProvider;
import com.ms.p2p.provider.LoanProvider;
import com.ms.p2p.provider.LoanTenderRecordProvider;
import com.ms.p2p.utils.JsonDataGridHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Created by chensk on 2017/7/14.
 */
@Controller("AdminLoanTenderController")
@RequestMapping("/admin/tender")
public class AdminLoanTenderController extends AdminBaseController{

    @Reference
    private LoanProvider loanProvider;
    @Reference
    private LoanTenderRecordProvider loanTenderRecordProvider;
    @Reference
    private LoanCategoryProvider loanCategoryProvider;

    @Reference
    private DictionaryProvider dictionaryProvider;

    @RequestMapping(value = {"/queryData"}, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public PageBean queryData(HttpServletRequest request, HttpSession session
            , @RequestParam(value = "userId", required = false) Integer userId,
                              @RequestParam(value = "borrowId", required = false) String borrowId,
                              PageBean page
    ) {
        page.setPageSize(1000);
        page.setList( loanTenderRecordProvider.findTenders(Integer.parseInt(borrowId)));
        return JsonDataGridHelper.createJSONData(page);
    }

    /**
     * 查看单个标的投标记录
     *
     * @param request
     * @param session
     * @return
     */
    @RequestMapping({"/tenderLog{borrowId}"})
    public String ViewBiddingRecordByBorrowId(HttpServletRequest request, HttpSession session, @PathVariable String borrowId) {
        request.setAttribute("borrowId", borrowId);
        return "admin/borrow/tenderLog";
    }

    @RequestMapping({"/exportTendersList"})
    public String exportTendersList(HttpServletRequest request, HttpSession session, Model model) {
        String borrowId = request.getParameter("borrowId");
        List<Map<String, Object>> tenders = loanTenderRecordProvider.findTenders(Integer.parseInt(borrowId));
        for (Map<String, Object> map : tenders) {
            Object u_userName = map.get("u_userName");
            if (u_userName != null) {
                map.put("u_userName", u_userName.toString());
            }
            Object real_name = map.get("real_name");
            if (real_name != null) {
                map.put("real_name", real_name.toString());
            }
            Object id = map.get("id");
            if (id != null) {
                map.put("id", id);
            }
            Object account = map.get("account");
            if (account != null) {
                map.put("account", account.toString());
            }
            Object interest = map.get("interest");
            if (interest != null) {
                map.put("interest", interest.toString());
            }
            Object addtime = map.get("addtime");
            if (interest != null) {
                map.put("addtime", MsDateUtils.TimeStamp2DateTime(addtime + ""));
            }

        }
        ExcelData excelData = new ExcelData();
        excelData.setHeads(new String[]{"用户名", "姓名", "记录编号", "金额",
                "利息", "时间"});
        excelData.setFieldNames(new String[]{"u_userName", "real_name", "id",
                "account", "interest", "addtime"});
        excelData.setDataList(tenders);
        excelData.setModelClass(Map.class);
        model.addAttribute("excelData", excelData);
        model.addAttribute("fileName", "标号" + borrowId + "的投标记录");
        return "excel";


    }


    @Override
    public boolean getBinderFlag() {
        return true;
    }
}
